﻿<script setup lang="ts" name="RoleManagement">
import { transformI18n } from '@/plugins/i18n'
import { getRoleList } from '@/api/role'
import RoleAddEdit from './components/roleAddEdit.vue'
import RoleDrawer from './components/roleDrawer.vue'
import useDictionaryEntry from '@/hooks/useDictionaryEntry'
const { getDictionaryEntryData } = useDictionaryEntry()
const statusList = ref([])
const pageData:any = ref({
  listData: {
    data: []
  },
  searchData: {
    title: '',
    status: 1
  }
})
// 重置页面数据
const resetPageData = () => {
  pageData.value.listData.data = []
  pageData.value.listData.total = 0
}
// 获取当前查询条件下的数据
const getInitData = () => {
  resetPageData()
  const result: any = {}
  result.name = pageData.value.searchData.title
  getRoleList(result, { config: { showLoading: false, mockEnable: true } }).then((res:any) => {
    console.log(res)
    if (res.data && res.data.length > 0) {
      setTimeout(() => {
        pageData.value.listData.data = res.data
      }, 1000)
    }
  })
}
// 查询条件重置
const resetSearch = () => {
  pageData.value.searchData.title = ''
  pageData.value.searchData.status = 1
  getInitData()
}

const roleDialog = ref(null)
// 新增事件
const add = () => {
  roleDialog.value.showDialog()
}
// 编辑事件
const edit = (item) => {
  roleDialog.value.showDialog(item)
}

const deleteRow = (v) => {
  const msg = transformI18n('page.确定删除吗？', true)
  ElMessageBox.alert(msg, transformI18n('moduleTitle.警告', true), {
    showCancelButton: true,
    confirmButtonText: transformI18n('buttons.buttonConfirm', true),
    cancelButtonText: transformI18n('buttons.buttonCancel', true),
    type: 'warning',
    callback: (action) => {
      if (action === 'confirm') {
        ElMessage({
          type: 'info',
          message: `操作: 点击了确认`
        })
      } else {
        ElMessage({
          type: 'info',
          message: `操作: 点击了取消`
        })
      }
    }
  })
}

const roleDrawer = ref(null)
const settingPermissions = (v) => {
  console.log(v)
  roleDrawer.value.showDrawer(v)
}

const getStatusList = async () => {
  statusList.value = await getDictionaryEntryData({
    label: '',
    status: 2,
    type: 'enable_number'
  })
}
getStatusList()
onMounted(() => {
  getInitData()
})
</script>

<template>
  <page-wrap direction="column">
    <el-form :inline="true">
      <el-form-item :label="$t('page.角色名称')">
        <el-input v-model="pageData.searchData.title" :placeholder="$t('placeholders.请输入') + $t('page.角色名称')" />
      </el-form-item>
      <el-form-item :label="$t('page.状态')">
        <el-radio-group class="radio-button" v-model="pageData.searchData.status">
          <el-radio-button v-for="(item, index) in statusList" :key="index" :label="item.value">{{ $t('page.' + item.label) }}</el-radio-button>
        </el-radio-group>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="getInitData">{{ $t('buttons.buttonSearch') }}</el-button>
        <el-button icon="Refresh" @click="resetSearch">{{ $t('buttons.buttonReset') }}</el-button>
        <el-button type="success" icon="Plus" @click="add()">{{ $t('buttons.buttonCreate') }}{{ $t('page.角色') }}</el-button>
      </el-form-item>
    </el-form>
    <div class="flex-1 table-layout">
      <div class="table-wrap">
        <el-table :data="pageData.listData.data" border>
          <el-table-column type="index" :label="$t('page.序号')" width="60" />
          <el-table-column prop="name" :label="$t('page.角色名称')" />
          <el-table-column prop="code" :label="$t('page.角色标识')" />
          <el-table-column prop="isAdmin" :label="$t('page.是否管理员')" width="120" align="center">
            <template #default="scope">
              <el-tag :type="scope.row.isAdmin ? 'success' : 'danger'">{{ scope.row.isAdmin ? $t('page.是') : $t('page.否') }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="status" :label="$t('page.状态')" width="90" align="center">
            <template #default="scope">
              <el-tag :type="scope.row.status ? 'success' : 'danger'">{{ scope.row.status ? $t('page.启用') : $t('page.停用') }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="sort" :label="$t('page.排序')" width="60" align="center" />
          <el-table-column prop="remark" :label="$t('page.备注')" />
          <el-table-column prop="operate" :label="$t('page.操作')" width="240" align="center">
            <template #default="scope">
              <el-button type="primary" icon="Edit" @click="edit(scope.row)"></el-button>
              <el-button type="danger" icon="Delete" @click="deleteRow(scope.row)"></el-button>
              <el-button type="warning" @click="settingPermissions(scope.row)">{{ $t('buttons.权限管理') }}</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <RoleAddEdit ref="roleDialog"></RoleAddEdit>
    <RoleDrawer ref="roleDrawer"></RoleDrawer>
  </page-wrap>
</template>

<style scoped lang="scss">
.table-layout {
  position: relative;
  margin: var(--el-space) 0 0;

  .table-wrap {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;

    .el-table {
      width: 100%;
      height: 100%;
    }
  }
}

.el-pagination {
  align-items: center;
  justify-content: flex-end;
  margin: var(--el-space) 0 0;
}
</style>
